package app.cms.action;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.utils.AnnexTool;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.mine.RandomUtil;
import org.limp.mine.StringTool;
import org.limp.mine.annex.AnnexInfo;
import org.limp.mine.annex.AnnexItem;

/**
 * 新闻图片管理Action
 * 
 * @author Bizkit
 * 
 */
public class InfoIMGAction extends DispatchAction {
	/**
	 * 插入图片到新闻
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward insertimg(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Connection conn = null;

		CommonBean4HSQ bean = new CommonBean4HSQ(request);
		Map para = bean.getResource();

		String uniqueid = StringTool.checkString(para.get("INFOUNIQUEID"));
		String infoid = StringTool.checkString(para.get("INFOID"));

		AnnexItem item = (AnnexItem) para.get(AnnexItem.ANNEX);

		if (!item.isEmpty()) {
			if (!infoid.equals("")) {
				item.setObjectId(infoid);
				item.setObjectName("APP_CMS_INFO");
			} else {
				//新建内容信息,尚无内容信息主键,使用uniqueid作为临时主键存储于xsystem_annex的FK_ANNEX_ID字段,附件关联对象亦为临时名称INSERT_IMG
				//内容信心成功提交保存后,InfoMachining会将附件关联对象信息重至为有效的值
				item.setObjectId(uniqueid);
				item.setObjectName("INSERT_IMG");
			}

			try {
				SystemDataSource ds = SystemDataSource.getInstance();

				conn = ds.getConnection();

				AnnexTool tool = new AnnexTool(conn);

				List list = tool.saveAnnex(item, true);

				conn.commit();

				AnnexInfo ai = tool.cloneAnnex(((AnnexInfo) list.get(0))
						.getID());

				request.setAttribute("uniqueid", uniqueid);
				request.setAttribute("img", ai);
			} catch (Exception ex) {
				ISystem.catchException(this, ex);
			} finally {
				SystemDataSource.closeConnection(conn);
			}
		}

		return mapping.findForward("insertimg");
	}

	/**
	 * 删除信息中的图片
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteimg(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Connection conn = null;

		CommonBean4HSQ bean = new CommonBean4HSQ(request);
		Map para = bean.getResource();

		String url = StringTool.checkString(para.get("oimgurl"));

		try {
			SystemDataSource ds = SystemDataSource.getInstance();

			conn = ds.getConnection();

			conn.commit();

			request.setAttribute("result", "1");
		} catch (Exception ex) {
			request.setAttribute("result", "-1");
			ISystem.catchException(this, ex);
		} finally {
			SystemDataSource.closeConnection(conn);
		}

		return mapping.findForward("deleteimg");
	}
}
